<?php

namespace frontend\modules\resource\models;

use common\models\UserBranch;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\BasMember;
use frontend\modules\common\models\BaseParam;

/**
 * BasMemberSearch represents the model behind the search form of `common\models\BasMember`.
 */
class BasMemberSearch extends BasMember
{
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'br_id', 'audit', 'isrule', 'reg_ip', 'login_ip', 'islog'], 'integer'],
            [['numbering', 'mini_openid', 'openid', 'unionid', 'avatar', 'phone', 'password', 'name', 'isrule_time', 'created_at', 'updated_at', 'deleted_at', 'id_number', 'user_name'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params, $branchId)
    {
        $query = BasMemberSearch::find()->joinWith([
            'userStatus' => function ($query) use ($branchId) {
                $query->andWhere(['hl_user_branch.br_id' => $branchId]);
            },
            'branchCount'
        ]);

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => BaseParam::$page,
            ],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'br_id' => $this->br_id,
            'audit' => $this->audit,
            'isrule' => $this->isrule,
            'isrule_time' => $this->isrule_time,
            'reg_ip' => $this->reg_ip,
            'login_ip' => $this->login_ip,
            'islog' => $this->islog,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            'deleted_at' => $this->deleted_at,
        ]);

        $query->andFilterWhere(['like', 'numbering', $this->numbering])
            ->andFilterWhere(['like', 'mini_openid', $this->mini_openid])
            ->andFilterWhere(['like', 'openid', $this->openid])
            ->andFilterWhere(['like', 'unionid', $this->unionid])
            ->andFilterWhere(['like', 'avatar', $this->avatar])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'password', $this->password])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'user_name', $this->user_name])
            ->andFilterWhere(['like', 'id_number', $this->id_number]);

        $query->andFilterWhere(['hl_user_branch.br_id' => $branchId]);
        return $dataProvider;
    }

    //关联user_branch
    public function getUserStatus()
    {
        return $this->hasOne(UserBranch::className(), ['user_id' => 'id']);
    }

    //关联user_branch
    public function getBranchCount()
    {
        return $this->hasMany(UserBranch::className(), ['user_id' => 'id']);
    }
}
